package com.tempjob.common.utils;

import com.tempjob.common.exception.BusinessException;
import com.google.common.hash.HashCode;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;

import java.nio.charset.Charset;
import java.security.MessageDigest;

public class MD5Util {

	 public static String hexMD5(String value) {
	        try {
	            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
	            messageDigest.reset();
	            messageDigest.update(value.getBytes("utf-8"));
	            byte[] digest = messageDigest.digest();
	            return byteToHexString(digest);
	        } catch (Exception ex) {
	            throw new RuntimeException(ex);
	        }
	    }

	 /**
	     * Write a byte array as hexedecimal String.
	     */
	    public static String byteToHexString(byte[] bytes) {
	        StringBuilder builder = new StringBuilder();
	        for (int i = 0; i < bytes.length; ++i) {
	            int v = bytes[i];
	            if (v < 0) {
	                v += 256;
	            }
	            String n = Integer.toHexString(v);
	            if(n.length() == 1)
	                n = "0" + n;
	            builder.append(n);
	        }
	        return builder.toString();
	    }


	private static HashFunction hf = Hashing.md5();
	private static Charset defaultCharset = Charset.forName("UTF-8");

	public static String md5(String data) {
		HashCode hash = hf.newHasher().putString(data, defaultCharset).hash();
		return hash.toString();
	}

	public static String md5(String data, Charset charset, boolean isUpperCase) {
		HashCode hash = hf.newHasher().putString(data, charset == null ? defaultCharset : charset).hash();
		return isUpperCase ? hash.toString().toUpperCase() : hash.toString();
	}

	public static String md5(byte[] bytes, boolean isUpperCase) {
		HashCode hash = hf.newHasher().putBytes(bytes).hash();
		return isUpperCase ? hash.toString().toUpperCase() : hash.toString();
	}

	public static String md5Cip(String plainText) {
		return md5(plainText.getBytes(), true);
	}

	/**
	 * 生成秘钥
	 * @param ptype
	 * @return
	 */
	public static String getSecretKey(String ptype){
		if("01".equals(ptype) || "02".equals(ptype)){
			return md5Cip("cardtypeWX03");
		}else if("03".equals(ptype)){
			return md5Cip("cardtypeWX04");
		}else{
			return md5Cip("cardtypeWX" + ptype);
		}
	}

	    /** ------------------ this below is add by negi ----------------------------- */
	    /**
	     * MD5加密数据
	     * @param pwd
	     * @return 16位密文
	     */
	    public static String getStr2Digest16(String pwd) {
	        return getStr2Digest32(pwd).substring(8, 24);
	    }

	    /**
	     * MD5加密数据
	     * @param pwd
	     * @return 32位密文
	     */
	    public static String getStr2Digest32(String pwd) {
	        String md5Str = "";
	        try {
	            MessageDigest md = MessageDigest.getInstance("MD5");
	            md.update(pwd.getBytes());
	            md5Str = toHex(md.digest());
	        } catch (NullPointerException e) {
	            throw new BusinessException("加密字符为空", e);
	        } catch (Exception e) {
	            throw new BusinessException("MD5 should be supported?", e);
	        }
	        return md5Str;
	    }

	    private static String toHex(byte buffer[]){
	        StringBuffer sb = new StringBuffer(buffer.length * 2);
	        for (int i = 0; i < buffer.length; i++){
	            sb.append(Character.forDigit((buffer[i] & 0xf0) >> 4, 16));
	            sb.append(Character.forDigit(buffer[i] & 0x0f, 16));
	        }
	        return sb.toString();
//	        StringBuilder hex = new StringBuilder(buffer.length * 2);
//	        for (byte b : buffer) {
//	            if ((b & 0xFF) < 0x10){
//	                hex.append("0");
//	            }
//	            hex.append(Integer.toHexString(b & 0xFF));
//	        }
//	        return hex.toString().toUpperCase();
	    }

}
